using AntDesign.ProLayout;
using Microsoft.AspNetCore.Components;
using NLog;
using NLog.Web;
using System.Net;

var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();


try
{
    var builder = WebApplication.CreateBuilder(args);
    builder.WebHost.ConfigureKestrel((context, serverOptions) =>
    {
        serverOptions.Listen(IPAddress.Loopback, 5003);
        // serverOptions.Listen(IPAddress.Loopback, 5001, listenOptions =>
        // {
        //     listenOptions.UseHttps("testCert.pfx", "testPassword");
        // });
    });
    // Add NLog for Logging
    builder.Logging.ClearProviders();
    builder.Host.UseNLog();
   
    // Add services to the container.
    builder.Services.AddRazorPages();
    builder.Services.AddServerSideBlazor();
    builder.Services.AddAntDesign();
    builder.Services.AddScoped(sp => new HttpClient
    {
        BaseAddress = new Uri(sp.GetService<NavigationManager>()!.BaseUri)
    });
    builder.Services.Configure<ProSettings>(builder.Configuration.GetSection("ProSettings"));

    var app = builder.Build();

// Configure the HTTP request pipeline.
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }

    app.UseHttpsRedirection();

    app.UseStaticFiles();

    app.UseRouting();

    app.MapBlazorHub();
    app.MapFallbackToPage("/_Host");

    app.Run();
}
catch (Exception exception)
{
    // NLog: catch setup errors
    logger.Error(exception, "Stopped program because of exception");
    throw;
}
finally
{
    LogManager.Shutdown();
}